package com.ittony.dao.impl;

import com.ittony.dao.SystemAdminDao;
import com.ittony.entity.SystemAdmin;
import com.ittony.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class SystemAdminDaoImpl implements SystemAdminDao {
    @Override
    public SystemAdmin findByUsername(String username) {
        Connection conn = JDBCUtil.getConnection();
        String sql = "select * from system_admin where username='"+username+"'";
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            statement = conn.prepareStatement(sql);
            resultSet = statement.executeQuery();
            if (resultSet.next()) {
                int id = resultSet.getInt(1);
                username = resultSet.getString(2);
                String password = resultSet.getString(3);
                String name = resultSet.getString(4);
                String telephone = resultSet.getString(5);
                SystemAdmin systemAdmin = new SystemAdmin();
                systemAdmin.setId(id);
                systemAdmin.setUsername(username);
                systemAdmin.setPassword(password);
                systemAdmin.setName(name);
                systemAdmin.setTelephone(telephone);
                return systemAdmin;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(conn, statement, resultSet);
        }
        return null;
    }
}
